<template>
  <div class="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control
      class="tab-control-hidden"
      :title="['流行', '新款', '精选']"
      @itemClick="tabClick"
      ref="tabControl1"
      v-show="isTabShowed"
    />

    <scroll
      class="wrapper"
      ref="scroll"
      :probe-type="3"
      :pull-up-load="true"
      @scroll="contentScroll"
      @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" />
      <home-recommend-view :recommends="recommend" />
      <home-feature-view />
      <tab-control
        class="tab-control"
        :title="['流行', '新款', '精选']"
        @itemClick="tabClick"
        ref="tabControl2"
      />
      <good-list :goods="showGoods" />
    </scroll>

    <back-top @click.native="backTopClick" v-show="isShow" />
  </div>
</template>
<script>
import NavBar from "components/common/navbar/NavBar.vue";
import TabBar from "components/common/tabbar/TabBar.vue";
import TabControl from "components/content/tabControl/tabControl.vue";
import GoodList from "../../components/content/goods/GoodList.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import BackTop from "components/content/backTop/BackTop.vue";

import HomeSwiper from "./childComps/HomeSwiper.vue";
import HomeRecommendView from "./childComps/HomeRecommendView.vue";
import HomeFeatureView from "./childComps/HomeFeatureView.vue";

import { getHomeMultidata, getHomeGoods } from "network/home.js";

export default {
  components: {
    NavBar,
    HomeSwiper,
    HomeRecommendView,
    HomeFeatureView,
    TabBar,
    TabControl,
    GoodList,
    Scroll,
    BackTop,
  },
  name: "Home",
  data() {
    return {
      banners: [], //轮播图数据
      recommend: [], //推荐数据
      goods: {
        //首页商品数据
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      isShow: false,
      tabOffsetTop: 0,
      isTabShowed: false,
      saveY: 0,
    };
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    },
  },
  created() {
    //在created中不要执行方法的具体操作，交到methods去执行
    // 1.请求首页轮播图的数据，getHomeMultidata返回一个promise
    this.created_getHomeMultidata();

    // 2.请求首页的上坪数据，输入数据的类型和页码
    this.created_getHomeGoods("pop");
    this.created_getHomeGoods("new");
    this.created_getHomeGoods("sell");
  },
  updated() {
    this.getTabOffsetTop();
  },
  destroyed() {
    console.log("destroyed");
  },
  activated() {
    // 返回到记录下的位置
    this.$refs.scroll.scroll.refresh();
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
  },
  deactivated() {
    //记录下离开时的位置
    this.saveY = this.$refs.scroll.scroll.y;
  },
  methods: {
    created_getHomeMultidata() {
      getHomeMultidata().then((res) => {
        this.banners = res.data.banner.list;
        this.recommend = res.data.recommend.list;
      });
    },

    created_getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list); //高阶函数
        this.goods[type].page += 1;
        this.$refs.scroll.finishPullUp();
      });
    },

    // 切换商品列表
    tabClick(index) {
      this.currentType = index == 0 ? "pop" : index == 1 ? "new" : "sell";
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },

    // 返回顶部
    backTopClick() {
      this.$refs.scroll.scrollTo(0, 0, 500); //scrollTo是一个封装的方法，面向组件
    },

    // 获取TabControl组件距离顶端的位置
    getTabOffsetTop() {
      // console.log(this.$refs.tabControl2);
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },

    // 监听滚动到的位置,决定backTop组件是否显示
    contentScroll(position) {
      // this.saveY = position.y;
      // console.log(this.saveY);

      if (-position.y > 1000) {
        this.isShow = true;
      } else {
        this.isShow = false;
      }
      if (this.tabOffsetTop > -position.y) {
        this.isTabShowed = false;
      } else {
        this.isTabShowed = true;
      }
    },

    // 上拉加载更多函数
    loadMore() {
      this.created_getHomeGoods(this.currentType);
    },
  },
};
</script>

<style scoped>
.home-nav {
  background-color: #ff8198;
}

.tab-control-hidden {
  position: fixed;
  left: 0;
  right: 0;
  top: 44px;
  background-color: #fff;
}

.wrapper {
  height: calc(100vh - 44px - 49px);
  /* height: 500px; */
  overflow: hidden;
}
</style>
